/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui;

import com.vaadin.addon.touchkit.ui.Popover;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;
import com.vaadin.ui.Window.ResizeEvent;
import org.processbase.touchprocess.ui.navigator.MenuNavigationManager;
import org.processbase.touchprocess.ui.template.MainView;
import org.processbase.touchprocess.ui.details.DetailsView;

public class TabletMainView extends MainView {

    public TabletMainView() {
        setSizeFull();
        addStyleName("tablet");
    }

    @Override
    public void attach() {
        super.attach();
        setOrientation();
        getWindow().addListener(this);
    }

    private void setOrientation() {
        removeAllComponents();
        menuNavigationManager = new MenuNavigationManager();
        detailsView = new DetailsView(false);
        if (isHorizontal()) {
            /*
             * Removed possible window currently showing the hierarchy view
             */
            addComponent(menuNavigationManager);
            addComponent(detailsView);
            detailsView.initUI();
            setExpandRatio(detailsView, 1);
        } else {
            btnMainNavigator = new Button();
            btnMainNavigator.addListener(this);

            addComponent(detailsView);
            btnMainNavigator.setCaption(menuNavigationManager.getCaption());

            HorizontalLayout hLayout = new HorizontalLayout();
            hLayout.setSpacing(true);
            hLayout.setHeight("30px");

            hLayout.addComponent(btnMainNavigator);

            detailsView.setLeftComponent(hLayout);
        }

        lastOrientationHorizontal = isHorizontal();
    }

    private boolean isHorizontal() {
        return getApplication().getMainWindow().getWidth() > getApplication().getMainWindow().getHeight();
    }

    @Override
    public void windowResized(ResizeEvent e) {
        if (getApplication() != null) {
            if (isHorizontal() != lastOrientationHorizontal) {
                setOrientation();
            }
        }
    }

    @Override
    public void buttonClick(ClickEvent event) {
        if (event.getButton() == btnMainNavigator) {
            Popover popover = new Popover();
            popover.setContent(menuNavigationManager);
            popover.setClosable(true);
            popover.showRelativeTo(btnMainNavigator);
            popover.setHeight(getParent().getHeight() - 100, UNITS_PIXELS);
            popover.addListener(new CloseListener() {

                public void windowClose(CloseEvent e) {
                    setEnabled(true);
                }
            });
        }

    }

    @Override
    public void setEnabled(boolean enabled) {
        detailsView.setEnabled(enabled);
    }
}
